package com.zrx.app.android.activity;

import android.icu.math.BigDecimal;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zrx.app.android.R;


/**
 * Created by qxb on 2017/8/11.
 */

public class StagMoneyActivity extends AppCompatActivity {
    private TextView twoYearStagMoneyTV;
    private TextView threeYearStagMoneyTV;
    private EditText loanMoneyET;
    private EditText twoInterestRateET;
    private EditText threeInterestRateET;
    private double dThreeInterestRate;
    private double dLoanMoney;
    private double dTwoInterestRate;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stag_money);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        loanMoneyET = (EditText) findViewById(R.id.loan_monet_et);
        twoInterestRateET = (EditText) findViewById(R.id.two_interest_rate_et);
        threeInterestRateET = (EditText) findViewById(R.id.three_interest_rate_et);
        /**
         * 给两年的利息添加文本改变监听
         */
        twoInterestRateET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                String loanMoney = loanMoneyET.getText().toString().trim();
                if ("".equals(loanMoney) || null == loanMoney) {
                    Toast.makeText(StagMoneyActivity.this, "请输入贷款金额", Toast.LENGTH_SHORT).show();
                    dLoanMoney=0.00;
                }else {
                    dLoanMoney = Double.parseDouble(loanMoney);//贷款金额
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String twoInterestRate = twoInterestRateET.getText().toString().trim();
                if (TextUtils.isEmpty(twoInterestRate)) {
                    twoInterestRate = "1.1632";
                }
                dTwoInterestRate = Double.parseDouble(twoInterestRate);//两年的利率
                double twoStagMoney = getStagMoney(dLoanMoney, dTwoInterestRate, 0);
                twoYearStagMoneyTV.setText(twoStagMoney + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        /**
         * 给三年的利息添加文本改变监听
         */
        threeInterestRateET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                String loanMoney = loanMoneyET.getText().toString().trim();
                if ("".equals(loanMoney) || null == loanMoney) {
                    Toast.makeText(StagMoneyActivity.this, "请输入贷款金额", Toast.LENGTH_SHORT).show();
                    dLoanMoney=0.00;
                }else {
                    dLoanMoney = Double.parseDouble(loanMoney);//贷款金额
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String threeInterestRate = threeInterestRateET.getText().toString().trim();
                if (TextUtils.isEmpty(threeInterestRate)) {
                    threeInterestRate = "1.2448";
                }
                dThreeInterestRate = Double.parseDouble(threeInterestRate);//三年的利率
                double threeStagMoney = getStagMoney(dLoanMoney, dThreeInterestRate, 1);
                threeYearStagMoneyTV.setText(threeStagMoney + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        /**
         * 给贷款金额添加文本改变监听
         */
        loanMoneyET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                String twoInterestRate = twoInterestRateET.getText().toString().trim();
                if (TextUtils.isEmpty(twoInterestRate)) {
                    twoInterestRate = "1.1632";
                }
                dTwoInterestRate = Double.parseDouble(twoInterestRate);//两年的利率
                String threeInterestRate = threeInterestRateET.getText().toString().trim();
                if (TextUtils.isEmpty(threeInterestRate)) {
                    threeInterestRate = "1.2448";
                }
                dThreeInterestRate = Double.parseDouble(threeInterestRate);//三年的利率
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String loanMoney = loanMoneyET.getText().toString().trim();
                if ("".equals(loanMoney) || null == loanMoney) {
                    Toast.makeText(StagMoneyActivity.this, "请输入贷款金额", Toast.LENGTH_SHORT).show();
                    dLoanMoney=0.00;
                }else {
                    dLoanMoney = Double.parseDouble(loanMoney);//贷款金额
                }
                double twoStagMoney = getStagMoney(dLoanMoney, dTwoInterestRate, 0);
                twoYearStagMoneyTV.setText(twoStagMoney + "");
                double threeStagMoney = getStagMoney(dLoanMoney, dThreeInterestRate, 1);
                threeYearStagMoneyTV.setText(threeStagMoney + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        twoYearStagMoneyTV = (TextView) findViewById(R.id.two_year_money_tv);

        threeYearStagMoneyTV = (TextView) findViewById(R.id.three_year_money_tv);

    }

    public double getStagMoney(double loan, double rate, int mark) {
        BigDecimal money=new BigDecimal(0.00);
        BigDecimal bg1=new BigDecimal(loan * rate);
        BigDecimal bg2=new BigDecimal(1.068);
        BigDecimal bg3=new BigDecimal(1.095);
        switch (mark) {
            case 0:
                money = bg1.divide(bg2,2,BigDecimal.ROUND_HALF_EVEN);
                break;
            case 1:
                money = bg1.divide(bg3,2,BigDecimal.ROUND_HALF_EVEN);
                break;
        }
        return money.doubleValue();
    }
}
